<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="课程名字" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名字"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程分类" prop="courseClassification">
        <el-select
          v-model="queryParams.courseClassification"
          placeholder="请选择课程分类"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in courseClassificationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程性质" prop="courseNature">
        <el-select
          v-model="queryParams.courseNature"
          placeholder="请选择课程性质"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in courseNatureOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程类型" prop="courseType">
        <el-select
          v-model="queryParams.courseType"
          placeholder="请选择课程类型"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in courseTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程软件" prop="courseSoftware">
        <el-select
          v-model="queryParams.courseSoftware"
          placeholder="请选择课程软件"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in courseSoftwareOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="课程难度" prop="courseDifficulty">
        <el-select
          v-model="queryParams.courseDifficulty"
          placeholder="请选择课程难度"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in courseDifficultyOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['link:course:add']"
          >新增课程信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['link:course:edit']"
          >修改课程信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['link:course:remove']"
          >删除课程信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['link:course:export']"
          >导出课程信息</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          :disabled="multiple"
          @click="handleActive({}, 1)"
          >批量上架</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          :disabled="multiple"
          @click="handleActive({}, 2)"
          >批量下架</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="courseList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程ID" width="64" align="center" prop="id" />
      <el-table-column label="课程名字" align="center" prop="courseName" />
      <el-table-column
        label="课程缩略图"
        align="center"
        prop="courseImage"
        width="128px"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.courseImage"
            style="width: 100%; display: block"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="课程分类"
        align="center"
        prop="courseClassification"
        :formatter="courseClassificationFormat"
      />
      <el-table-column
        label="课程性质"
        align="center"
        prop="courseNature"
        :formatter="courseNatureFormat"
      />
      <el-table-column
        label="课程类型"
        align="center"
        prop="courseType"
        :formatter="courseTypeFormat"
      />
      <el-table-column
        label="课程软件"
        align="center"
        prop="courseSoftware"
        :formatter="courseSoftwareFormat"
      />
      <el-table-column
        label="课程难度"
        align="center"
        prop="courseDifficulty"
        :formatter="courseDifficultyFormat"
      />
      <el-table-column
        width="64"
        label="学员"
        align="center"
        prop="learnCount"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['link:course:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['link:course:remove']"
            >删除</el-button
          >
          <br />
          <el-button
            size="mini"
            type="text"
            icon="el-icon-upload2"
            @click="handleActive(scope.row, 1)"
            v-hasPermi="['link:course:remove']"
            >上架</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleActive(scope.row, 2)"
            v-hasPermi="['link:course:remove']"
            >下架</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程基本信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="128px">
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="课程图片路径"
              prop="courseImage"
              style="position: relative"
            >
              <!-- <el-input v-model="form.courseImage" placeholder="请输入课程团片路径" /> -->
              <el-upload
                action="http://122.114.5.144:92/common/upload"
                list-type="picture-card"
                :show-file-list="false"
                :limit="1"
                :on-success="handleSuccess"
                :on-remove="handleRemove"
                :before-upload="beforeAvatarUpload"
                ref="upload"
              >
                <i slot="default" class="el-icon-plus"></i>
                <div slot="tip" class="el-upload__tip">
                  只能上传jpg/png文件，且不超过500kb
                </div>
              </el-upload>
              <div class="showImg" v-if="form.courseImage">
                <img :src="form.courseImage" />
              </div>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20"
            ><el-form-item label="课程名字" prop="courseName">
              <el-input
                v-model="form.courseName"
                placeholder="请输入课程名字"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="20"
            ><el-form-item label="课程描述" prop="courseDescribe">
              <el-input
                v-model="form.courseDescribe"
                type="textarea"
                placeholder="请输入内容"
              /> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程分类" prop="courseClassification">
              <el-select
                v-model="form.courseClassification"
                placeholder="请选择课程分类"
              >
                <el-option
                  v-for="dict in courseClassificationOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程性质" prop="courseNature">
              <el-select
                v-model="form.courseNature"
                placeholder="请选择课程性质"
              >
                <el-option
                  v-for="dict in courseNatureOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="课程类型" prop="courseType">
              <el-select v-model="form.courseType" placeholder="请选择课程类型">
                <el-option
                  v-for="dict in courseTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="课程软件" prop="courseSoftware">
              <el-select
                v-model="form.courseSoftware"
                placeholder="请选择课程软件"
              >
                <el-option
                  v-for="dict in courseSoftwareOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="课程难度" prop="courseDifficulty">
              <el-select
                v-model="form.courseDifficulty"
                placeholder="请选择课程难度"
              >
                <el-option
                  v-for="dict in courseDifficultyOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12">
            <el-form-item label="任课老师" prop="courseTeacher">
              <el-select
                v-model="form.teacherIds"
                multiple
                placeholder="请选择任课老师"
              >
                <el-option
                  v-for="dict in courseTeacherOptions"
                  :key="dict.userId"
                  :label="dict.userName"
                  :value="dict.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="是否限时免费" prop="isTimeLimit">
              <el-select
                v-model="form.isTimeLimit"
                placeholder="请选择是否限时免费"
              >
                <el-option
                  v-for="dict in isTimeLimitOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="是否免费" prop="isFree">
              <el-select v-model="form.isFree" placeholder="请选择是否免费">
                <el-option
                  v-for="dict in isFreeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12"
            ><el-form-item label="是否基础课" prop="isBasis">
              <el-select v-model="form.isBasis" placeholder="请选择是否基础课">
                <el-option
                  v-for="dict in isBasisOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12"
            ><el-form-item label="是否是首页封面" prop="isCover">
              <el-select
                v-model="form.isCover"
                placeholder="请选择是否是首页封面"
              >
                <el-option
                  v-for="dict in isCoverOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select> </el-form-item
          ></el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="课程金额" prop="courseMoney">
              <el-input
                v-model="form.courseMoney"
                placeholder="请输入课程金额"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="学习人数(预设)">
              <el-input
                v-model="form.learnCount"
                placeholder="请输入"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="课程标签" prop="courseTags">
              <el-select
                v-model="form.labelIds"
                multiple
                placeholder="请选择课程标签"
              >
                <el-option
                  v-for="dict in courseTagsOptions"
                  :key="dict.dictCode"
                  :label="dict.dictLabel"
                  :value="dict.dictCode"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24"
            ><el-form-item label="课程备注" prop="remark">
              <editor v-model="form.remark" :min-height="192" /> </el-form-item
          ></el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCourse,
  getCourse,
  delCourse,
  addCourse,
  updateCourse,
  exportCourse,
  getTeachers,
  getTags,
  ActCourseOn,
  ActCourseOff,
} from "@/api/link/course";
import Editor from "@/components/Editor";
export default {
  name: "Course",
  components: {
    Editor,
  },
  data() {
    return {
      imgdv: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 课程表格数据
      courseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 课程分类字典
      courseClassificationOptions: [],
      // 课程性质字典
      courseNatureOptions: [],
      // 课程类型字典
      courseTypeOptions: [],
      // 课程软件字典
      courseSoftwareOptions: [],
      // 课程难度字典
      courseDifficultyOptions: [],
      // 课程老师字典
      courseTeacherOptions: [],
      // 课程标签字典
      courseTagsOptions: [],
      // 是否限时免费字典
      isTimeLimitOptions: [],
      // 是否免费字典
      isFreeOptions: [],
      // 是否基础课字典
      isBasisOptions: [],
      // 是否是首页封面字典
      isCoverOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        courseName: null,
        courseClassification: null,
        courseNature: null,
        courseType: null,
        courseSoftware: null,
        courseDifficulty: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        courseName: [
          { required: true, message: "课程名字不能为空", trigger: "blur" },
        ],
        courseClassification: [
          { required: true, message: "课程分类不能为空", trigger: "change" },
        ],
        courseNature: [
          { required: true, message: "课程性质不能为空", trigger: "change" },
        ],
        courseType: [
          { required: true, message: "课程类型不能为空", trigger: "change" },
        ],
        courseSoftware: [
          {
            required: true,
            message: "课程软件不能为空",
            trigger: "change",
          },
        ],
        courseDifficulty: [
          { required: true, message: "课程难度不能为空", trigger: "change" },
        ],
        courseDescribe: [
          { required: true, message: "课程描述不能为空", trigger: "blur" },
        ],
        courseMoney: [
          { required: true, message: "金额不能为空", trigger: "blur" },
        ],
        isTimeLimit: [
          {
            required: true,
            message: "是否限时免费不能为空",
            trigger: "change",
          },
        ],
        isFree: [
          { required: true, message: "是否免费不能为空", trigger: "change" },
        ],
        isBasis: [
          { required: true, message: "是否基础课不能为空", trigger: "change" },
        ],
        isCover: [
          {
            required: true,
            message: "是否是首页封面不能为空",
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getList();
    this.getDicts("course_classification").then((response) => {
      this.courseClassificationOptions = response.data;
    });
    this.getDicts("course_nature").then((response) => {
      this.courseNatureOptions = response.data;
    });
    this.getDicts("course_type").then((response) => {
      this.courseTypeOptions = response.data;
    });
    this.getDicts("course_software").then((response) => {
      this.courseSoftwareOptions = response.data;
    });
    this.getDicts("course_difficulty").then((response) => {
      this.courseDifficultyOptions = response.data;
    });
    getTeachers().then((response) => {
      this.courseTeacherOptions = response.data;
    });
    getTags().then((response) => {
      this.courseTagsOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isTimeLimitOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isFreeOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isBasisOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.isCoverOptions = response.data;
    });
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isPNG = file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG && !isPNG) {
        this.$message.error("上传头像图片只能是 JPG 或者 PNG 格式");
        return false;
      }
      // if (!isLt2M) {
      //   this.$message.error('上传图片大小不能超过 2MB!');
      // }
      // return isJPG && isLt2M && isPNG;
      // return isJPG && isPNG;
    },
    handleSuccess(res, file) {
      this.form.courseImage = res.url;
      this.$refs.upload.clearFiles();
    },
    handleRemove(file) {
      console.log(file);
    },
    /** 查询课程列表 */
    getList() {
      this.loading = true;
      listCourse(this.queryParams).then((response) => {
        this.courseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 课程分类字典翻译
    courseClassificationFormat(row, column) {
      return this.selectDictLabel(
        this.courseClassificationOptions,
        row.courseClassification
      );
    },
    // 课程性质字典翻译
    courseNatureFormat(row, column) {
      return this.selectDictLabel(this.courseNatureOptions, row.courseNature);
    },
    // 课程类型字典翻译
    courseTypeFormat(row, column) {
      return this.selectDictLabel(this.courseTypeOptions, row.courseType);
    },
    // 课程软件字典翻译
    courseSoftwareFormat(row, column) {
      return this.selectDictLabel(
        this.courseSoftwareOptions,
        row.courseSoftware
      );
    },
    // 课程难度字典翻译
    courseDifficultyFormat(row, column) {
      return this.selectDictLabel(
        this.courseDifficultyOptions,
        row.courseDifficulty
      );
    },
    // 是否限时免费字典翻译
    isTimeLimitFormat(row, column) {
      return this.selectDictLabel(this.isTimeLimitOptions, row.isTimeLimit);
    },
    // 是否免费字典翻译
    isFreeFormat(row, column) {
      return this.selectDictLabel(this.isFreeOptions, row.isFree);
    },
    // 是否基础课字典翻译
    isBasisFormat(row, column) {
      return this.selectDictLabel(this.isBasisOptions, row.isBasis);
    },
    // 是否是首页封面字典翻译
    isCoverFormat(row, column) {
      return this.selectDictLabel(this.isCoverOptions, row.isCover);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.upload.clearFiles();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseImage: null,
        courseName: null,
        courseClassification: null,
        courseNature: null,
        courseType: null,
        courseSoftware: null,
        courseDifficulty: null,
        courseDescribe: null,
        courseMoney: null,
        learnCount: null,
        isTimeLimit: null,
        isFree: null,
        isBasis: null,
        isCover: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加课程基本信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getCourse(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改课程基本信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateCourse(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCourse(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      console.log(ids);
      return;
      this.$confirm('是否确认删除课程编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delCourse(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 上下架按钮操作 */
    handleActive(row, type) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认上架课程编号为"' + ids + '"的数据项?', "提醒", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          let temp = ids
          if (type == 1) {
            return ActCourseOn(temp);
          }
          if (type == 2) {
            return ActCourseOff(temp);
          }
        })
        .then(() => {
          this.getList();
          if (type == 1) {this.msgSuccess("上架成功");}
          if (type == 2) {this.msgSuccess("下架成功");}
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有课程数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportCourse(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
 <style lang="scss" scoped>
.showImg {
  position: absolute;
  top: 0;
  left: 160px;
  width: 256px;
  height: 148px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f8f8f8;
  > img {
    max-width: 100%;
    max-height: 100%;
  }
}
</style>
